package com.taoyibao.hongcanting.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.taoyibao.hongcanting.R;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;


/**
 * Glide工具类
 */
public class GlideUtil {

    private static GlideUtil mInstance;

    private GlideUtil() {
    }

    public static GlideUtil getInstance() {
        if (mInstance == null) {
            synchronized (GlideUtil.class) {
                if (mInstance == null) {
                    mInstance = new GlideUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * Glide API
     *
     * @param context
     * @param url
     * @param options
     * @param imageView
     */
    private void loadImageApi(Context context, String url, RequestOptions options, ImageView imageView) {
        Glide.with(context).load(url)// 加载图片
//                .crossFade()// 设置淡入淡出效果，默认300ms，可以传参
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)// 缓存修改过的图片
                .apply(options)
                .into(imageView);
    }

    /**
     * 使用默认的占位符和默认的错误图片
     */
    public void loadDefaultImage(Context context, String url, ImageView imageView) {
        loadImageApi(context, url, getDefaultOptions(), imageView);
    }

    /**
     * 圆形图片
     */
    public void loadDefaultCircleHead(Context context, String url, ImageView imageView) {
        loadImageApi(context, url, getCircleHeadOptions(), imageView);
    }


    /**
     * 获取默认的站位图RequestOptions
     *
     * @return
     */
    private RequestOptions getDefaultOptions() {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.place_hoder)//图片加载出来前，显示的图片
                .fallback(R.drawable.place_hoder) //url为空的时候,显示的图片
                .error(R.drawable.error_img);//图片加载失败后，显示的图片
        return options;
    }

    /**
     * 获取圆形头像默认的站位图RequestOptions
     *
     * @return
     */
    private RequestOptions getCircleHeadOptions() {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.default_touxiang)//图片加载出来前，显示的图片
                .fallback(R.drawable.default_touxiang) //url为空的时候,显示的图片
                .error(R.drawable.default_touxiang)//图片加载失败后，显示的图片
                .circleCropTransform();
        return options;
    }

    /**
     * 加载圆角图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadRoundCircleImage(Context context, String url, ImageView imageView, int radius) {
        RequestOptions options = new RequestOptions()
                .circleCrop()//设置圆形
                .placeholder(R.drawable.place_hoder)
                .error(R.drawable.error_img)
                .bitmapTransform(new RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.ALL))
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        loadImageApi(context, url, options, imageView);
    }


}
